package base;

import java.util.logging.Level;
import java.util.logging.Logger;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import static org.lwjgl.opengl.GL11.*;
import org.newdawn.slick.Font;
import org.newdawn.slick.TrueTypeFont;

public class Graphics {
	public static Font currentFont;
	public static int fontTextureID;
	
	/**
	* Sets graphics mode.
	* Provide width and height of screen in pixels and unit size in pixels for camera.

	* @see #parallax example
	*/
	public static void init( int width, int height, double unitSize, boolean loadFont ) {
		try {
			Display.setDisplayMode( new DisplayMode( width, height ) );
			Display.create();
		} catch ( LWJGLException ex ) {
			Logger.getLogger( Graphics.class.getName() ).log( Level.SEVERE, null, ex );
		}

		glShadeModel( GL_SMOOTH );
		glDisable( GL_DEPTH_TEST );
		glDisable( GL_LIGHTING ); 
		
		glEnable( GL_BLEND );
		glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
		glEnable( GL_TEXTURE_2D );
		
		glMatrixMode( GL_PROJECTION ) ;
		glLoadIdentity();
		glOrtho( 0d, width / unitSize, height / unitSize, 0d, -1d, 1d );
		glMatrixMode( GL_MODELVIEW ) ;
		
		try {
			Mouse.create();
		} catch ( LWJGLException ex ) {
			Logger.getLogger( Graphics.class.getName() ).log( Level.SEVERE, null, ex );
		}
		
		try {
			java.awt.Font font = new java.awt.Font("Times New Roman", java.awt.Font.BOLD, 16 );
			currentFont =  new TrueTypeFont( font, false );
			fontTextureID = glGetInteger( GL_TEXTURE_BINDING_2D );
		} catch (Exception e) {
			
		}
	}
	
	public static void setColor( float red, float green, float blue ) {
		glColor4d( red, green, blue, 1d );
	}
	
	
	public static void drawRectangle( double x, double y, double width, double height, double angle, boolean empty ){
		if( empty ) {
			glDisable( GL_TEXTURE_2D );
			
			glBegin( GL_LINE_LOOP );
			glVertex2d( x, y );
			glVertex2d( x + width, y );
			glVertex2d( x + width, y + height );
			glVertex2d( x, y + height );
			glEnd();
			
			glEnable( GL_TEXTURE_2D );
		} else {
			glDisable(GL_TEXTURE_2D);
			
			glRectd( x, y, x + width, y + height );
		
			glEnable( GL_TEXTURE_2D ); 
		}
	}
	
	public static void drawRectangle( double x, double y, double width, double height ){
		drawRectangle( x, y, width, height, 0d, false );
	}
	
	public static void drawEmptyRectangle( double x, double y, double width, double height ){
		drawRectangle( x, y, width, height, 0d, true );
	}
	

	public static void drawText( String string, float x, float y ) {
		//glEnable( GL_TEXTURE_2D );
		//glBindTexture( GL_TEXTURE_2D, fontTextureID );
		currentFont.drawString( x, y, string );
	}
	
	public static void drawText( String string, double x, double y ) {
		drawText( string, (float) x, (float) y );
	}
	

	public static void swapBuffers() {
		Display.update();
	}
}
